package com.study.mynew.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.study.mynew.R;
import com.study.mynew.adapter.NewItemAdapter;
import com.study.mynew.bean.NewItemBean;
import com.study.mynew.bean.NewsData;

import org.litepal.LitePal;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class CollectionActivity extends BaseActivity {
    private NewItemAdapter mAdapter ;

    private RecyclerView  rv_collection;
    private TextView tvHint;
    private ImageButton delete_btn,BackBtn;

    @Override
    public int initView() {
        return R.layout.activity_collection;
    }

    @Override
    public void initData() {
        rv_collection = findViewById(R.id.collect_news);
        tvHint = findViewById(R.id.tvHint);
        delete_btn = findViewById(R.id.delete_btn);
        BackBtn = findViewById(R.id.BackBtn);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv_collection.setLayoutManager(linearLayoutManager);
        mAdapter = new NewItemAdapter();
        rv_collection.setAdapter(mAdapter);
        initNews();
    }

    @Override
    public void initListener() {


        delete_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LitePal.deleteAll(NewItemBean.class, "fromCollection=?", "1");
                Toast.makeText(CollectionActivity.this, "清空浏览记录！", Toast.LENGTH_SHORT).show();
                initNews();
            }
        });

        BackBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initNews() {
        List<NewItemBean> newItemBeans = LitePal.where("fromCollection=?", "1").find(NewItemBean.class);
        mAdapter.setNewInstance(newItemBeans);
    }
    @Override
    public void onResume() {
        super.onResume();
        initNews();
    }
}